<?php
/**
 * 这个来演示继承和组合的区别
 * User: ford
 * Date: 15-6-16
 * Time: 上午10:31
 */

/**
 * 下面是继承方式
 * Class DoMath
 */
class DoMath {
	private $sum;
	private $quotient;

	/**
	 * 求和
	 * @param $first
	 * @param $second
	 * @return mixed
	 */
	public function simpleAdd($first, $second) {
		$this->sum = $first + $second;

		return $this->sum;
	}

	/**
	 * 求商
	 * @param $dividend
	 * @param $divisor
	 * @return float
	 */
	public function simpleDivide($dividend, $divisor) {
		$this->quotient = $dividend / $divisor;

		return $this->quotient;
	}
}

/**
 * 下面这个类用于增加文本功能，
 */
class InheritMath extends DoMath {
	private $textOut;
	private $fullFace;

	public function numToText($num) {
		$this->textOut = (string)$num;

		return $this->textOut;
	}

	public function addFace($face, $msg) {
		$this->fullFace = '<strong>' . $face .'</strong>:' . $msg;

		return $this->fullFace;
	}
}

/**
 * 下面是Client入口类，它只需要实例化InheritMath类，也可以使用DoMath类的功能
 */
class Client{
	private $added;
	private $divided;
	private $textNum;
	private $output;

	public function __construct() {
		$family = new InheritMath();	//只实例化子类
		$this->added = $family->simpleAdd(40, 60);
		$this->divided = $family->simpleDivide($this->added, 25);
		$this->textNum = $family->numToText($this->divided);
		$this->output = $family->addFace('Your results', $this->textNum);

		echo $this->output;
	}
}

$worker = new Client();